package com.visionblue.business.ship.page.collection.record

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.visionblue.business.ship.R
import com.visionblue.business.ship.model.bean.local.VolumeErrorInfo
import com.visionblue.business.ship.util.FormatUtils
import kotlinx.android.extensions.LayoutContainer
import kotlinx.android.synthetic.main.item_not_record.*

/**
 * @author: xlh
 * @date: 2020-07-22 9:54 AM
 * @desc:
 */
class NotRecordAdapter : RecyclerView.Adapter<NotRecordAdapter.VH>() {

    var onItemClickListener: OnItemClickListener? = null

    private val mData = mutableListOf<VolumeErrorInfo>()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
        val itemView =
            LayoutInflater.from(parent.context).inflate(R.layout.item_not_record, parent, false)
        return VH(itemView)
    }

    override fun getItemCount() = mData.size

    override fun onBindViewHolder(holder: VH, position: Int) {

        val volumeErrorInfo = mData[position]
        holder.tvVolume.text = "${FormatUtils.format3Decimal(volumeErrorInfo.errVal.toDouble())} m³"
        holder.tvPipeName.text = "来自${volumeErrorInfo.alias}"

        holder.btnHandle.setOnClickListener {
            mData.remove(volumeErrorInfo)
            onItemClickListener?.onItemClick(volumeErrorInfo.type)
            notifyItemRemoved(holder.layoutPosition)
        }
    }


    fun setData(infos: List<VolumeErrorInfo>) {
        mData.clear()
        mData.addAll(infos)
        notifyDataSetChanged()
    }

    class VH(override val containerView: View) : RecyclerView.ViewHolder(containerView),
        LayoutContainer

    interface OnItemClickListener {
        fun onItemClick(type: Int)
    }
}